<template>
  <div class="container">
    <Paglist
      v-model="dataList"
      :is-init-api="true"
      :parame="parame"
      @register="register"
      :scroll-top-bottom="260"
      :is-show-loadmore="dataList.length >= 1"
    >
      <template v-for="v in dataList" :key="v.id">
        <view class="list-item">
          <BusinessOpportunitiesItem
            v-bind="v"
            @edit="handleEdit(v)"
            @set="handleSet(v)"
            @del="handleDel(v)"
            @release="handleRelease(v)"
            @detail="handleDetail(v)"
            @follow="handleFollow(v)"
            @share="handleShare(v)"
            @close="handleClose(v)"
          ></BusinessOpportunitiesItem>
        </view>
      </template>
    </Paglist>
  </div>
</template>
<script setup lang="ts">
  import { closeBoSys, delBo, publishBo, sysBoInfoList } from '@/api/businessOpportunities';
  import type {
    IBusinessOpportunityList,
    ISysBoInfoListQuery,
  } from '@/api/businessOpportunities/type';
  import Paglist from '@/components/Paglist/index.vue';
  import { useConList } from '@/components/Paglist/useConList';
  import { ref, provide, type PropType, watch } from 'vue';
  import BusinessOpportunitiesItem from './BusinessOpportunitiesItem.vue';
  import { navigateTo } from '@/router';
  import { ReleaseTypeEnum } from '@/enums/businessOpportunitiesEnum';
  import { Toast } from '@/utils';

  const props = defineProps({
    parame: {
      type: Object as PropType<ISysBoInfoListQuery>,
      default: () => ({
        cooperationTypeId: null,
        industryCategoryIdList: [],
        keyWord: '',
        current: 1,
        size: 10,
      }),
    },
  });
  const emits = defineEmits(['set', 'share']);
  provide('api', async (obj: ISysBoInfoListQuery) => {
    const res = await sysBoInfoList(obj);
    return res;
  });
  const dataList = ref<IBusinessOpportunityList[]>([]);

  const [register, { refresh }] = useConList();

  const handleShare = (v: IBusinessOpportunityList) => emits('share', v);

  const handleEdit = (v: IBusinessOpportunityList) => {
    navigateTo({
      name: 'releaseBusinessOpportunities',
      options: {
        id: v.id,
        releaseType: ReleaseTypeEnum.insteadMember,
        success: () => refresh(),
      },
    });
  };

  const handleRelease = async (v: IBusinessOpportunityList) => {
    await publishBo(v.id, ReleaseTypeEnum.insteadMember);
    Toast('已发布');
    refresh();
  };

  const handleFollow = (v: IBusinessOpportunityList) => {
    navigateTo({
      name: 'followUpRecords',
      options: {
        boInfoId: v.id,
      },
    });
  };

  const handleClose = async (v: IBusinessOpportunityList) => {
    await closeBoSys(v.id);
    refresh();
    Toast('已关闭');
  };
  const handleDetail = (v: IBusinessOpportunityList) => {
    navigateTo({
      name: 'supplyDemandDetails',
      options: {
        id: v.id,
        successFn: () => {
          refresh();
        },
      },
    });
  };

  const handleDel = (v: IBusinessOpportunityList) => {
    delBo(v.id).then(() => {
      refresh();
      Toast('已删除草稿');
    });
  };
  const handleSet = (v: IBusinessOpportunityList) => {
    emits('set', v);
  };

  watch(
    () => props.parame,
    () => {
      refresh();
    },
    { deep: true, immediate: true },
  );

  defineExpose({
    refresh,
  });
</script>
<style lang="scss" scoped>
  .list-item {
    padding: 0 30rpx;
    margin-bottom: 30rpx;
    &:nth-child(1) {
      padding-top: 30rpx;
    }
  }
</style>
